<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">点击封装</button>
    <script>
        function ajax(url) {
            return new Promise((resolve, reject) => {
                let xhr = new XMLHttpRequest();
                xhr.open('get', url)
                xhr.onreadystatechange = () => {
                    if (xhr.readyState == 4) {
                        if (xhr.status >= 200 && xhr.status < 300) {
                            resolve(JSON.parse(xhr.responseText))
                        } else {
                            reject(new Error('404'))
                        }
                    }
                }
                xhr.send()
            })
        }
        document.querySelector('#btn').onclick = async function () {
            // let p = ajax('http://localhost:3000/movie2')
            // p.then(velue => {
            //     console.log(p);
            // }, error => {
            //     console.log(error.message);
            // })
            try {
                let p = await ajax('http://localhost:3000/movie');
                console.log(p);
            } catch (e) {
                console.log(e.message)
            }

        }
    </script>
</body>

</html>